﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SimpleSM
{
    public partial class TypeSession : Form
    {
        private int id = 0;

        public TypeSession(int id = 0)
        {
            InitializeComponent();
            this.id = id;
            if (this.id != 0)
            {
                DataTable dt = new TSession().Get(this.id);
                tbName.Text = dt.Rows[0][1].ToString();
                tbAppPath.Text = dt.Rows[0][2].ToString();
                tbArgs.Text = dt.Rows[0][3].ToString();
                tbPort.Text = dt.Rows[0][4].ToString();
                btAddToAll.Enabled = true;
                btRemoveFromAllSessions.Enabled = true;
            }
        }

        private void btAppSelect_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                tbAppPath.Text = openFileDialog1.FileName;
            }
        }

        private void tbPort_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !Char.IsNumber(e.KeyChar) && e.KeyChar != 8;
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            if (tbName.Text == "")
            {
                MessageBox.Show("Name cannot be emplty!", "Stop", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }
            if (tbAppPath.Text == "")
            {
                MessageBox.Show("Select application!", "Stop", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }
            if (tbPort.Text == "")
            {
                tbPort.Text = "0";
            }
            if (id == 0)
            {
                new TSession().Add(tbName.Text, tbAppPath.Text, tbArgs.Text, int.Parse(tbPort.Text));
            }
            else
            {
                new TSession().Update(this.id, tbName.Text, tbAppPath.Text, tbArgs.Text, int.Parse(tbPort.Text));
            }
            this.Close();
        }

        private void btRemoveFromAllSessions_Click(object sender, EventArgs e)
        {

        }

        private void btAddToAll_Click(object sender, EventArgs e)
        {

        }
    }
}
